package com.gen.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gen.service.ILogService;
import com.gen.service.IUserService;
import com.zzx.entity.Log;
import com.zzx.entity.User;

@Controller
public class UserController {
	@Autowired
	private  IUserService UserService;
	
	@Autowired
	private ILogService logService;
	
	@Autowired
	private HttpSession Session;
	
	@RequestMapping(value = "/userpage.do")
	public ModelAndView userPage() {
		ModelAndView modelAndView = new ModelAndView();
//		查询个人信息
		Integer uid = (int) Session.getAttribute("uid");
		User user = UserService.selectUserInfoById(uid);
		System.out.println(user);
		modelAndView.addObject("user", user);
		
//		查登陆的日志
		List<Log> logs = logService.selectLogsByUid(uid);
		modelAndView.addObject("logs", logs);
		modelAndView.setViewName("userPage");
		return modelAndView;
	}
	
	@RequestMapping("/toUpdateUser.do")
	public ModelAndView toUpdateUser(@RequestParam("uid") Integer uid) {
		ModelAndView modelAndView = new ModelAndView();
		User user = UserService.selectUserInfoById(uid);
		modelAndView.addObject("user", user);
		modelAndView.setViewName("updateUser");
		return modelAndView;
	}
	@RequestMapping("/updateUser.do")
	@ResponseBody
	public Integer updateUser(@RequestBody User user) {
		System.out.println(user);
		int result = UserService.updateUserInfo(user);
		return result;
	}
	
}
